import { defineStore } from "pinia"

/**
 * 应用设置状态管理
 * 管理应用主题、语言等全局设置
 */

export const useAppStore = defineStore('app', () => {
    // 状态
    const theme = ref('light')
    const language = ref('zh-CN')
  
    // 操作方法
    function setTheme(newTheme: 'light' | 'dark') {
      theme.value = newTheme
      localStorage.setItem('theme', newTheme)
    }
  
    function setLanguage(newLang: string) {
      language.value = newLang
      localStorage.setItem('language', newLang)
    }
  
    // 初始化
    function init() {
      const savedTheme = localStorage.getItem('theme')
      if (savedTheme) {
        theme.value = savedTheme as 'light' | 'dark'
      }
      
      const savedLang = localStorage.getItem('language')
      if (savedLang) {
        language.value = savedLang
      }
    }
  
    return {
      theme,
      language,
      setTheme,
      setLanguage,
      init
    }
  })